//
//  SinaHomeCell.m
//  MySinaWeiboProjectForOC
//
//  Created by apple on 2016/11/17.
//  Copyright © 2016年 WangChen. All rights reserved.
//

#import "SinaHomeCell.h"
#import "SinaHomePictureCell.h"
#import <UIImageView+WebCache.h>
#import <SDWebImageManager.h>

@interface SinaHomeCell ()<UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UIImageView *iv_icon;
@property (weak, nonatomic) IBOutlet UILabel *lb_name;
@property (weak, nonatomic) IBOutlet UILabel *lb_time;
@property (weak, nonatomic) IBOutlet UILabel *lb_source;
@property (weak, nonatomic) IBOutlet UILabel *lb_content;
@property (weak, nonatomic) IBOutlet UIImageView *iv_vip;
@property (weak, nonatomic) IBOutlet UIImageView *iv_rank;


@property (weak, nonatomic) IBOutlet UICollectionView *pictureView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *constraintH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *constraintW;

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *pictureFlowLaypout;

@property (weak, nonatomic) IBOutlet UILabel *lb_retweeterContent;





@end

static NSString *cellID = @"cellID";

static CGFloat imageMargin = 2;

@implementation SinaHomeCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.iv_icon.layer.cornerRadius = 35;
    self.iv_icon.layer.masksToBounds = YES;
    self.lb_content.preferredMaxLayoutWidth = kScreenWidth - 2 * kMargin;
    self.lb_retweeterContent.preferredMaxLayoutWidth = kScreenWidth - 2 * kMargin;
    
    self.pictureView.dataSource = self;
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //注册单元格
    UINib *nib = [UINib nibWithNibName:@"SinaHomePictureCell" bundle:nil];
    [self.pictureView registerNib:nib forCellWithReuseIdentifier:cellID];
    
    CGFloat maxW = kScreenWidth - 2 * kMargin;
    CGFloat itemW = (maxW - 2 * imageMargin) / 3;
    
    self.pictureFlowLaypout.itemSize = CGSizeMake(itemW, itemW);
    self.pictureFlowLaypout.minimumLineSpacing = imageMargin;
    self.pictureFlowLaypout.minimumInteritemSpacing = imageMargin;
}




#pragma mark
#pragma mark - homeCellViewModel的set方法
-(void)setHomeCellViewModel:(SinaHomeCellViewModel *)homeCellViewModel{
    _homeCellViewModel = homeCellViewModel;
    
    [self.iv_icon sd_setImageWithURL:homeCellViewModel.iconURL];
    self.lb_name.text = homeCellViewModel.homeCellModel.user.name;
    self.lb_time.text = homeCellViewModel.creatDateStr;
    self.lb_source.text = homeCellViewModel.messageSource;
    self.lb_content.text = homeCellViewModel.homeCellModel.text;
    self.iv_vip.image = homeCellViewModel.vipImage;
    self.iv_rank.image = homeCellViewModel.rankImage;
    
    //根据获得的数据计算picture的宽高
//    CGSize pictureViewSize = [self getPictureViewSizeWithPictureCount:homeCellViewModel.pictureUrlInfoModelArray.count];
    CGSize itemSize;
    CGSize pictureViewSize = [self getPictureViewSizeWithPictureCount:homeCellViewModel.pictureUrlInfoModelArray.count andItemSize:&itemSize];

    self.constraintH.constant = pictureViewSize.height;
    self.constraintW.constant = pictureViewSize.width;
    self.pictureFlowLaypout.itemSize = itemSize;
    
    [self layoutIfNeeded];
    if(homeCellViewModel.homeCellModel.retweeted_status != nil){
        self.lb_retweeterContent.text = homeCellViewModel.homeCellModel.retweeted_status.text;
    }
    
    
    [self.pictureView reloadData];
}

#pragma mark
#pragma mark - 根据获得的数据计算pictureView的宽高
-(CGSize)getPictureViewSizeWithPictureCount:(NSInteger)count andItemSize:(CGSize *)itemSize{
    
    CGFloat maxW = kScreenWidth - 2 * kMargin;
    CGFloat itemW = (maxW - 2 * imageMargin) / 3;
    
    if(count == 1){
        UIImage *downLoadImage = [[SDWebImageManager sharedManager].imageCache imageFromDiskCacheForKey:self.homeCellViewModel.pictureUrlInfoModelArray.firstObject.thumbnail_pic];
        *itemSize = downLoadImage.size;
        return *itemSize;
    }
    
    if(count == 4){
        CGFloat w = 2 * itemW + imageMargin + 1;
        *itemSize = CGSizeMake(itemW, itemW);
        return CGSizeMake(w, w);
    }
    if(count == 0){
        *itemSize = CGSizeZero;
        return CGSizeZero;
    }
    
    NSInteger row = (count - 1) / 3 + 1;
    CGFloat maxH = row * itemW + (row - 1) * imageMargin;
    *itemSize = CGSizeMake(itemW, itemW);
    return CGSizeMake(maxW, maxH);
}





#pragma mark
#pragma mark - uicollection的数据源
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.homeCellViewModel.pictureUrlInfoModelArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SinaHomePictureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    
    NSString *pictUrlStr = self.homeCellViewModel.pictureUrlInfoModelArray[indexPath.item].thumbnail_pic;
    
    cell.imageURLStr = pictUrlStr;
    
    return cell;
    
    
    
}



@end
